package cn.com.dcsgo.config;

import cn.com.dcsgo.properties.Knife4jProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@RequiredArgsConstructor
@Slf4j
@ConditionalOnClass(WebMvcConfigurationSupport.class)
@Profile("dev")
public class Knife4jConfig {

    private final Knife4jProperties knife4jProperties;

    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docketAdmin() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title(knife4jProperties.getTitle())
                .version(knife4jProperties.getVersion())
                .description(knife4jProperties.getDescription())
                .contact(new Contact(knife4jProperties.getContact().get(0), knife4jProperties.getContact().get(1), knife4jProperties.getContact().get(2)))
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName(knife4jProperties.getGroupName())
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(knife4jProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
        log.info("knife4j配置成功"+knife4jProperties.getContact());
        return docket;
    }
}
